<?php error_reporting(E_ALL);
require_once('functions.php');?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

	<title> College Hustler </title>
	<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
	<link rel="stylesheet" type="text/css" href="./style.css" />


	<script src="./js/jquery.js" type="text/javascript"></script>
		<script src="./js/prototype.js" type="text/javascript"></script>
		<script src="./js/scriptaculous.js" type="text/javascript"></script>

		
		<script type="text/javascript">

		jQuery.noConflict();

			var count = 0;

			var pages = <?php echo $pages; ?>;

			if(pages == 0){
				if(total <= 39){
					pages = 1;
				}
			}

			var sql = "<?php echo $sql; ?>";

			var total = <?php echo $rowCount; ?>;

			var first = 40;

			jQuery(window).scroll(function(){

				if(jQuery(window).scrollTop() == jQuery(document).height() - jQuery(window).height()){

					jQuery('div#loaded').html('Content being loaded...');

					jQuery('div#loadmoreajaxloader').show();

					if(pages > 0){

						if(pages > count){

							jQuery.ajax({
								url: "scroll.php?sql=" + sql + "&first=" + first,
								success: function(html){

											if(html){
												jQuery("#postswrapper").append(html);
												jQuery('div#loadmoreajaxloader').hide();
												count++;
											}else{
												jQuery('div#loadmoreajaxloader').html('<center>No more posts to show.</center>');
											}
								}

							});
						first = first + 10;
						} else {
							jQuery('div#loadmoreajaxloader').html('<center>No more posts to show.</center>');
						}
					} else {
						jQuery('div#loadmoreajaxloader').html('<center>No more posts to show.</center>');
					}
				}
			});
			
		function likeItem(likeId){
			jQuery.ajax({
				url: "likeItem.php?id=" + likeId,
				success: function(html){
					jQuery('div#likeButton' + likeId).hide();
					jQuery('div#likeButton' + likeId).html('<b>You like this</b>');
				}
			});
			return false;
		}
		
		function viewItem(id){
			jQuery.ajax({
			  url: "viewItem.php?id=" + id,
			})
		}
		
		function goHome(){
			//Set to 1==1 for home sign to work
			if(1==2){ window.location = '<?= getUrl() ?>'; }
		}
		
		function validate(){
			var description=document.forms["post"]["description"].value.length;
			var title=document.forms["post"]["name"].value.length;
			var price=document.forms["post"]["price"].value;
			var disclaimer = document.forms["post"]["disclaimer"].checked;
			
			
			if(description > 2300){
				alert("Your description must be less than 2300 characters, you have " + description + " characters currently");
				return false;
			}
			
			if(title > 50){
				alert("Your title must be less than 50 characters, you have " + title + " characters currently");
				return false;
			}
			
			if(isNaN(price)){
				alert("The price you entered " + price + " is not a number, try again. (remove comma's)");
				return false;
			}
			
			if(price.length > 6){
				alert("The price you entered (" + price + ") is too large, let's be realistic now");
				return false;
			}
			
			if(disclaimer == false){
				alert("You need to agree to the terms of use (displayed to the right), to post things on College Hustler");
				return false;
			}
			
		}
		
		function post(post){
			Effect.toggle('ROW-' + post, 'slide'); 
		}
		
		</script>

	</head>
<body>
	
<?php if($debug){ ?>
	<div style="background-color:#d0ddcf;border: 1px solid #9CAA9C; margin-top:0px;">
		<b>DEBUGGING PANEL:</b> - 
		<?php echo 'college (' . $college . ') - sort (' . $sort . ') - search (' . $search . ') - LoggedIn (' . $loggedIn . ')'; ?>
		<?php echo liUser() . '@' . liDomain() . ' - ID(' . liId() . ')'; ?>
		<?php echo ' - SQL(' . $sql . ')' . ' - searchParts(' . $searchParts . ')'; ?>
		<?php echo ' - ' . $_SERVER['REMOTE_ADDR']; ?>
	</div>
<?php } ?>

	<div class="loginBar" name="loginbar">

			<?php 
			if($loggedIn){
				echo '<a href="userPosts.php">';
				echo '<i class="icon-user icon-white"></i>&nbsp;' . liUser() . '@' . liDomain() . '</a>';
				
				if(liLevel()=='admin'){
					echo '<a href="admin/index.php"> / <i class="icon-wrench icon-white"></i>&nbsp; Admin';
				}
				
				echo ' / <a href="logout.php"><i class="icon-road icon-white"></i>&nbsp;Logout</a></b>';
			} else {
				echo '<a href="./login.php"><i class="icon-lock icon-white"></i><b>Login/ Register</b></a>';
			}
			
			?>
			
		</a>
		
	</div>
		
		<?php if(isLi() == false) { ?>
			<?php if(isset($_COOKIE['firstVisit'])){
				$display = 'display:none;';
			} else {
				$display = '';
			} ?>		
	<?php } else if(isLi()){ }?>
	
	<h2 class="muted">Turtle Content Management System</h2>
	<div class="row-fluid">
		<div class="span12 top" onclick="goHome()">
			<b><a href="<?php echo getUrl(); ?>">Home</a></b>
			
			<?php 
			
			if($college == 'all'){
				echo '<b> > <a href="' . getUrl() . '"> All Colleges</a></b>';
				
				if($search!='empty'){
					echo '<b> > Search "' . $search . '"</b>';
					echo  '<a href="' . getUrl() . '"> <small>(clear search)</small></a>';
				}
				
			} else {
				echo '<b> > <a href="' . getUrl() . '?college=' . $college . '">' . getCollegeName($college) . '</a> </b>';
				
				if($search!='empty'){
					echo '<b> > Search "' . $search . '"</b>';
					echo  '<b><a href="' . getUrl() . '?college=' . $college . '"> <small>(clear search)</small></a></b>';
				}
				
				if($sort!='notSet'){
					echo '<b>';
					if($sort=="priceHighLow"){
						echo ' > Price Sorted High to Low';
					} else if($sort=='priceLowHigh'){
						echo ' > Price Sorted Low to High';
					} else if($sort=='hitsAsc'){
						echo ' > Likes Sorted Low to High';
					} else if($sort=='hitsDesc'){
						echo ' > Likes Sorted Low to High';
					}
					echo '</b>';
				}
			} 
			if($item!='empty'){
				echo '<b> > Item id ' . $item . '</b>';
			} else if($rowCount > 0){
				echo '<b> > ' . $rowCount . ' items found</b>';
			} else {
				echo '<b> > 0 items found </b>';
			}
			
			
			?>

	</div>

	


	
